package org.ccu.eshop.controller.api;


import com.alibaba.fastjson.JSON;
import com.mysql.jdbc.log.LogFactory;
import org.ccu.eshop.domain.dto.CartVo;
import org.ccu.eshop.domain.dto.MemberVo;
import org.ccu.eshop.service.MemberServiceItface;
import org.ccu.eshop.service.ShopCarServiceltface;
import org.ccu.eshop.service.impl.MemberServiceimpl;
import org.ccu.eshop.service.impl.ShopCarServiceimpl;
import org.ccu.eshop.utils.AjaxResultUtil;
import org.ccu.eshop.utils.LogOutputUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@WebServlet("/api/member")
public class MemberController extends HttpServlet {
    ShopCarServiceltface shopCarService=new ShopCarServiceimpl();
    MemberServiceItface memberService=new MemberServiceimpl();
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //super.doPost(req, resp);
        String method=request.getParameter("m");
        if("login".equals(method)){
            login(request,response);
        }else if("getLoginMember".equals(method)) {
            getLoginMember(request,response);
        }else  if(method.equals("exit")){
                exit(request,response);
        }
    }

    private void exit(HttpServletRequest request, HttpServletResponse response)  throws ServletException, IOException {
    //清空session
        HttpSession session=request.getSession();
        if (session!=null){
            session.invalidate();
            // session.removeAttribute("loginMember");
        }
       AjaxResultUtil result=AjaxResultUtil.success("会员安全退出成功");
        response.getWriter().write(JSON.toJSONString(result));
    }

    /**
     * 获得 登录用户信息
     * @param request
     * @param response
     */
    private void getLoginMember(HttpServletRequest request, HttpServletResponse response)  throws ServletException, IOException{
        HttpSession session=request.getSession();
        Object obj= session.getAttribute("loginMember");
            if (obj!=null){
                MemberVo memberVo=(MemberVo)obj;
                AjaxResultUtil  result=AjaxResultUtil.success("session存在信息，登录成功",memberVo);
                response.getWriter().write(JSON.toJSONString(result));
            }else {
                AjaxResultUtil  result=AjaxResultUtil.error(502,"session中不存在信息---");
                response.getWriter().write(JSON.toJSONString(result));
            }
    }

    private void login(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
        String loginName=request.getParameter("loginName");
        String loginPwd=request.getParameter("loginPwd");
        //调用控制器 把用户名和密码传过去】返回一个memberVo
         MemberVo memberVo= memberService.login(loginName,loginPwd);
        AjaxResultUtil result=null;
         if(memberVo!=null){
             //创建购物车  获得
            CartVo cartVo=shopCarService.getShopCar(memberVo.getId());
             LogOutputUtil.logger.info(loginName+"登录成功");
                result=AjaxResultUtil.success("登录成功",memberVo);
                //将用户信息存到session中
             HttpSession session=  request.getSession(true);
             session.setAttribute("loginMember",memberVo);
             session.setAttribute("shopCart",cartVo);
         }else {
             LogOutputUtil.logger.error(loginName+"登录失败");
             result=AjaxResultUtil.error(501,"用户名或密码错误");
         }
         response.getWriter().write(JSON.toJSONString(result));
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }
}
